<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16 0016
 * Time: 15:11
 */

namespace Admin\Controller;




class RuleController extends AuthController{

    public function index(){
        $rule = M('auth_rule');
        $count      = $rule->count();
        $Page       = new \Think\Page($count,5);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $show       = $Page->show();
        //dump($show);
        $rules = $rule->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('rule',$rules);
        $this->assign('page',$show);
        $this->display('rule');
    }

    public function addrule(){
        if(IS_POST){
            //dump(I('post.')) ;exit;
            $rules = I('post.');
            //dump($rules);exit;
            $users = D('auth_rule');
            if($users->create()){
                if($users->add($rules)){
                    $this->success('添加成功',U('rule/index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($users->getError());
            }
        }
        $auth = M('auth_rule')->select();
        $this->assign('auths' , $auth);
        $this->display('addrule');
    }

    public function editrule($id){
        $rule = D('auth_rule');
        $rules = $rule->find($id);
        if(IS_POST){
            $post = I('post.');
            if($rule->create()){
                if($rule->save($post)){
                    $this->success('编辑成功',U('rule/index'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($rule->getError());
            }
        }
        $this->assign('rules',$rules);
        $this->display('editrule');
    }

    public function delrule($id){
        $rule = D('auth_rule');
        $rules = $rule->delete($id);
        if($rules){
            $this->success('删除成功！' , U('rule/index'));
        }else{
            $this->error('删除失败');
        }
    }

}
